var uid, ws, spage, gid;
document.addEventListener('plusready',function(){
	ws = plus.webview.currentWebview();
	spage = ws.spage;
	console.log(spage);
	uid = plus.storage.getItem('uid');//用户id
	gid = plus.storage.getItem('gid');//用户类型
	var user = plus.storage.getItem('user');//登录手机号
	var avatar = plus.storage.getItem('userAvatar') != 'null'?getAvatar(plus.storage.getItem('userAvatar')):'../images/iconic.png';//用户头像
	var name = plus.storage.getItem('name') != ''?plus.storage.getItem('name'):user;//用户名称
	var sex = plus.storage.getItem('sex') != 'null'?plus.storage.getItem('sex'):'';//用户性别
	var age = plus.storage.getItem('age') != '0'?plus.storage.getItem('age'):'';//用户年龄
	var marry = plus.storage.getItem('marry') != 'null'?plus.storage.getItem('marry'):'';//婚否
	var height = plus.storage.getItem('height')?plus.storage.getItem('height'):'';//身高
	var weight = plus.storage.getItem('weight')?plus.storage.getItem('weight'):'';//体重
	var nation = plus.storage.getItem('nation') != 'null'?plus.storage.getItem('nation'):'';//用户民族
	var phone = plus.storage.getItem('phone') != 'null'?plus.storage.getItem('phone'):'';//用户手机号
	var address = plus.storage.getItem('address') != 'null'?plus.storage.getItem('address'):'';//用户地址
	$('.uploadPic').attr('src',avatar);
	$('.name').val(name);
	$('.sex').val(sex);
	$('.age').val(age);
	$('.marry').val(marry);
	$('.height').val(height);
	$('.weight').val(weight);
	$('.nation').val(nation);
	$('.phone').val(phone);
	$('.address').val(address);
	if(gid == '4'){	//医生
		$('.time').show();
		var surgery = plus.storage.getItem('surgery') != 'null'?plus.storage.getItem('surgery'):'';//出诊时间
		$('.surgery').val(surgery);
	}
	edit();
},false)
function edit(){
	$('.mui-input-row').each(function(){
		$(this).find("input,textarea").prop("disabled",true).css({"color":"#ccc"});
	})
	$(".bj").on('tap',function(){
		if($(this).text()=="编辑"){
			$('.mui-input-row').each(function(){
				$(this).find("input,textarea").prop("disabled",false).css({"color":"#000"});
			});
			$(this).text('完成');
		}else{
			var name = $('.name').val();
			var sex = $('.sex').val();
			var age = $('.age').val();
			var marry = $('.marry').val();
			var height = $('.height').val();
			var weight = $('.weight').val();
			var nation = $('.nation').val();
			var phone = $('.phone').val();
			var address = $('.address').val();
			var surgery = $('.surgery').val();
			if(!phone.match(p1)){
				toast('手机号格式错误');return;
			}
			if(sex != '男' && sex != '女'){
				toast('性别错误');return;
			}
			if(!age.match(a)){
				toast('年龄错误');return;
			}
			if(height && !height.match(h)){
				toast('身高错误');return;
			}
			if(height && !weight.match(w)){
				toast('体重错误');return;
			}
			showWating();
			$.ajax({
				type:"get",
				url:apiRoot + "?m=home&c=member&a=edituserInfo",
				data:{
					name :name,
					sex :sex,
					age :age,
					marry:marry,
					height :height,
					weight :weight,
					nation :nation,
					phone :phone,
					address :address,
					surgery :surgery,
					uid :uid
				},
				success :function(data){
					$('.bj').text('编辑');
					$("input,textarea").each(function(){
						$(this).prop("disabled",true);
					})
					plus.nativeUI.closeWaiting();
					console.log(JSON.stringify(data));
					if(spage){
						plus.webview.getWebviewById(spage).reload();
					}
					plus.storage.setItem('name',name+'');//用户名称
					plus.storage.setItem('sex',sex+'');//用户性别
					plus.storage.setItem('age',age+'');//用户年龄
					plus.storage.setItem('marry',marry+'');//婚否
					plus.storage.setItem('height',height+'');//身高
					plus.storage.setItem('weight',weight+'');//体重
					plus.storage.setItem('nation',nation+'');//用户民族
					plus.storage.setItem('phone',phone+'');//用户手机号
					plus.storage.setItem('address',address+'');//用户地址
					plus.storage.setItem('surgery',surgery+'');//出诊时间
//					console.log(phone+" "+address);
				},
				error :function(e){
					$('.bj').text('编辑');
					console.log(JSON.stringify(e));
					errortoast();
				}
			});
		}
	})
	//修改头像
	$('.uploadPic').on('tap', function() {
		mui('#sheet1').popover('toggle');
	})
	//选择拍照添加
	$('.paizhao').on('tap', function(){
		var cmr = plus.camera.getCamera(); 
		cmr.captureImage( function(path){
			$('.uploadPic').attr('src',plus.io.convertLocalFileSystemURL(path));
			appendPic(path);
		}, function(err){}, {index:1} );   
	})
	//选择相册添加
	$('.photo').on('tap', function(){
		plus.gallery.pick( function(path){
		 	$('.uploadPic').attr('src',path);
		 	appendPic(path);
		}, function(error){}, {} );
	})
    var index = 1;
	var files = [];
	// 添加照片
	function appendPic(p) {
		files.push({name:"uploadkey"+index,path:p});
		index++;
		upload();
	}
	//创建对像
	function upload(){
//		console.log(apiRoot+'?m=Home&c=Member&a=uploadPic');
		var wt = plus.nativeUI.showWaiting('处理中');  
		var  task = plus.uploader.createUpload(apiRoot+'?m=Home&c=Member&a=uploadPic&id='+uid,
		{ method:"POST"},
		function ( t, status ) {  
			// 上传完成     
			if ( status == 200 ) {
				var pic = t.responseText;
				console.log(pic);
				if(pic == -1){
					toast('上传失败');
				}else{
					$(".uploadPic").attr('src',webRoot+t.responseText);
					plus.storage.setItem("userAvatar",t.responseText+"");
					if(spage){
						plus.webview.getWebviewById(spage).reload();
					}
				}
				wt.close(); 
			} else {
				plus.nativeUI.toast( "上传失败："+status );
				wt.close();
			}  
		});
//		console.log(JSON.stringify(task));
		for(var i=0;i<files.length;i++){
			var f=files[i];
			task.addFile(f.path,{key:f.name});
		}
		task.start();
	}
}
